const { IntentStack, IntentObject, Intent, INTENT_THROW, INTENT_SYNC_STACK, INTENT_STACK, INTENT_STOP, INTENT_THROWUP } = require("./IntentStack");




//append ==================================================
let Behavior = class Behavior extends IntentStack{
    constructor(intentObj,data){
        super(intentObj,data);
        this.setStackName(this.constructor.name);
        if(this.do != undefined && this.do.constructor.name == "Function"){
            this.do.apply(this,data);
        }
    }

    todo(obj,...data){

        if(IntentStack.isPrototypeOf(obj)){
            data.unshift(obj);
            let intent = new Intent();
            intent.setAction(INTENT_STACK);
            intent.setData(data);
            this.push(intent);
            return;
        }
        
    }

    sync(obj,...data){

        if(IntentStack.isPrototypeOf(obj)){
            data.unshift(obj);
            let intent = new Intent();
            intent.setAction(INTENT_SYNC_STACK);
            intent.setData(data);
            this.push(intent);
            return;
        }
    }

    toNext(){
        this.getIntentObject().next();
    }

    stop(time = 1){
        this.push(new Intent(INTENT_STOP,time));
    }

    throw(type){
        this.push(new Intent(INTENT_THROW,type));
    }

    throwUp(type){
        this.push(new Intent(INTENT_THROWUP,type));
    }
}
let CreepBehavior = class CreepBehavior extends Behavior{

    getCreep(){
        try{
            return this.getIntentObject().getControlObj();
        }catch(e){
            return;
        }
    }

    getCreepName(){
        try{
            return this.getIntentObject().name;
        }catch(e){
            return;
        }
    }

    //== API
    build(target){
        this.push(new Intent('build',target));
    }

    harvest(target){
        this.push(new Intent('harvest',target));
    }
    
    move(direction){
        this.push(new Intent('move',direction));
    }

    repair(target){
        this.push(new Intent('repair',target));
    }

    say(msg){
        this.push(new Intent('say',msg))
    }

    transfer(target,type,amount){
        this.push(new Intent('transfer',target,type,amount));
    }

    upgradeController(target){
        this.push(new Intent('upgradeController',target));
    }

    withdraw(target, resourceType, amount){
        this.push(new Intent('withdraw',target,resourceType,amount));
    }

    
}
let IntentCreep = class IntentCreep extends IntentObject{

    constructor(name){
        super();
        this.name = name;
    }
    
    //Abstract Override
    getControlObj(){
        return Game.creeps[this.name];
    }
}

module.exports.Behavior = Behavior;
module.exports.CreepBehavior = CreepBehavior;
module.exports.IntentCreep = IntentCreep;